package com.hiwhitley.easy.day02;

import java.util.Stack;

/**
 * 由两个栈组成的队列
 * 题目要求：编写一个类，用两个栈实现队列，支持队列的基本操作（add，poll，peek）
 *
 */

/**
 * Created by hiwhitley on 2016/5/24.
 */
public class TwoStackQueue<T> {
    private Stack<T> stackPush;
    private Stack<T> stackPop;

    public TwoStackQueue() {
        stackPush = new Stack<>();
        stackPop = new Stack<>();
    }

    public void add(T item) {
        stackPush.push(item);
    }

    public T poll() {
        if (stackPop.empty() && stackPush.empty()) {
            throw new RuntimeException("Queue is empty");
        } else if (stackPop.empty()) {
            while (!stackPush.empty()) {
                stackPop.push(stackPush.pop());
            }
        }

        return stackPop.pop();
    }

    public T peek() {
        if (stackPop.empty() && stackPush.empty()) {
            throw new RuntimeException("Queue is empty");
        } else if (stackPop.empty()) {
            while (!stackPush.empty()) {
                stackPop.push(stackPush.pop());
            }
        }

        return stackPop.peek();
    }


}
